package com.yjs.app.trade.web.controller.business.statistics;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yjs.app.trade.core.util.DateUtil;
import com.yjs.app.trade.core.util.MongoUtil;
import com.yjs.app.trade.core.util.StringUtils;
import com.yjs.app.trade.entity.base.StatisticsConstant;
import com.yjs.app.trade.web.controller.base.BaseController;
import com.yjs.app.trade.web.controller.base.util.Result;

import net.sf.json.JSONObject;

/**
 * 发票相关控制层
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/app/business/statistics/buyer")
public class BuyerStatisticsController extends BaseController {
	
	/**
	 * 获取医疗机构30天内每天的订单、支付、收货金额
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/buyerOrderPriceInfoThirtyDays", method = RequestMethod.GET)
	@ResponseBody
	public Object buyerOrderPriceInfoThirtyDays(String data, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("buyerId", getUserMemberId(request));
		List<String> projection = new ArrayList<String>();
		projection.add("buyerId");
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		sorts.put("created", 1);
		List<String> list = MongoUtil.getListInfo(StatisticsConstant.BuyerCollection.BUYERORDERPRICEINFOTHIRTYDAYS, map, projection, null, null, sorts);
		List<String> dates = DateUtil.getLastDates(new Date(), 30, Calendar.DATE, DateUtil.dateFormat);
		boolean flag = false;
		for (String string : dates) {
			flag = false;
			for (String json : list) {
				JSONObject jsonObject = JSONObject.fromObject(json);
				if (jsonObject.get("created") != null) {
					if (StringUtils.equals(string, jsonObject.getString("created"))) {
						flag = true;
					}
				}
			}
			if (!flag) {
				list.add("{\"created\":\"" + string + "\"}");
			}
		}
		list.sort(new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				JSONObject jsonObject1 = JSONObject.fromObject(o1);
				JSONObject jsonObject2 = JSONObject.fromObject(o2);
				if (jsonObject1.get("created") != null && jsonObject2.get("created") != null ) {
					try {
//						System.out.println(jsonObject1 + "&" + jsonObject2);
						Date date1 = DateUtil.dateFormat.parse(jsonObject1.getString("created"));
						Date date2 = DateUtil.dateFormat.parse(jsonObject2.getString("created"));
						return DateUtil.compare_date(date1, date2);
					} catch (ParseException e) {
						e.printStackTrace();
						logger.error("买方日期排序出错:" + jsonObject1 + "&" + jsonObject2);
					}
				}
				return 0;
			}
		});
		return Result.csuccess(list);
	}
	/**
	 * 年度总采购额
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/buyerOrderTotalPriceYear", method = RequestMethod.GET)
	@ResponseBody
	public Object buyerOrderTotalPriceYear(String data, HttpServletRequest request) {
		return Result.csuccess(MongoUtil.composeNormalById(
				"buyerId", getUserMemberId(request), "buyerId",  StatisticsConstant.BuyerCollection.BUYERORDERTOTALPRICEYEAR));
	}
	/**
	 * 近12月采购金额
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/buyerOrderTotalPriceTwelveMonths", method = RequestMethod.GET)
	@ResponseBody
	public Object buyerOrderTotalPriceTwelveMonths(String data, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("buyerId", getUserMemberId(request));
		List<String> projection = new ArrayList<String>();
		projection.add("buyerId");
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		sorts.put("years", 1);
		sorts.put("months", 1);
		List<String> list = MongoUtil.getListInfo(StatisticsConstant.BuyerCollection.BUYERORDERTOTALPRICETWELVEMONTHS, map, projection, null, null, sorts);
		List<String> dates = DateUtil.getLastDates(new Date(), 12, Calendar.MONTH, DateUtil.dateFormatMonth);
		String years = "";
		String months = "";
		boolean flag = false;
		for (String string : dates) {
			flag = false;
			for (String json : list) {
				JSONObject jsonObject = JSONObject.fromObject(json);
				if (jsonObject.get("years") != null && jsonObject.get("months") != null) {
					years = jsonObject.getString("years");
					months = jsonObject.getString("months");
					if (StringUtils.equals(string, years + "-" + months) ) {
						flag = true;
					}
				}
			}
			if (!flag) {
				String year = string.split("\\-")[0];
				String month = string.split("\\-")[1];
				list.add("{\"years\":\"" + year + "\", \"months\":\"" + month + "\", \"lastYears\":\"" + (Integer.parseInt(year) - 1) + "\", \"lastMonths\":\"" + month + "\"}");
			}
		}
		list.sort(new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				JSONObject jsonObject1 = JSONObject.fromObject(o1);
				JSONObject jsonObject2 = JSONObject.fromObject(o2);
				if (jsonObject1.get("years") != null && jsonObject2.get("years") != null &&
						jsonObject1.get("months") != null && jsonObject2.get("months") != null) {
					int year1 = jsonObject1.getInt("years");
					int year2 = jsonObject2.getInt("years");
					int month1 = jsonObject1.getInt("months");
					int month2 = jsonObject2.getInt("months");
					if (year1 > year2) {
						return 1;
					} else if (year1 == year2) {
						if (month1 > month2) {
							return 1;
						} else {
							return -1;
						}
					} else {
						return -1;
					}
				} else {
					return 0;
				}
			}
		});
		return Result.csuccess(list);
	}
	/**
	 * 一年基药比
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/buyerOrderDistrictRatioYear", method = RequestMethod.GET)
	@ResponseBody
	public Object buyerOrderDistrictRatioYear(String data, HttpServletRequest request) {
		return Result.csuccess(MongoUtil.composeNormalById(
				"buyerId", getUserMemberId(request), "buyerId",  StatisticsConstant.BuyerCollection.BUYERORDERDISTRICTRATIOYEAR));
	}
	/**
	 * 一年备案比
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/buyerOrderRecordRatioYear", method = RequestMethod.GET)
	@ResponseBody
	public Object buyerOrderRecordRatioYear(String data, HttpServletRequest request) {
		return Result.csuccess(MongoUtil.composeNormalById(
				"buyerId", getUserMemberId(request), "buyerId",  StatisticsConstant.BuyerCollection.BUYERORDERRECORDRATIOYEAR));
	}
	/**
	 * 一年合同采购比
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/buyerOrderContractRatioYear", method = RequestMethod.GET)
	@ResponseBody
	public Object buyerOrderContractRatioYear(String data, HttpServletRequest request) {
		return Result.csuccess(MongoUtil.composeNormalById(
				"buyerId", getUserMemberId(request), "buyerId",  StatisticsConstant.BuyerCollection.BUYERORDERCONTRACTRATIOYEAR));
	}
	
	/**
	 * 采购量前十
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/buyerOrderNumProductTopTen", method = RequestMethod.GET)
	@ResponseBody
	public Object buyerOrderNumProductTopTen(String data, HttpServletRequest request) {
		JSONObject jsonObject = dpackData(data);
		String type = jsonObject.getString("type");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("buyerId", getUserMemberId(request));
		map.put("type", type);
		List<String> projection = new ArrayList<String>();
		projection.add("buyerId");
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		sorts.put("sort", 1);
		return Result.csuccess(MongoUtil.getListInfo(StatisticsConstant.BuyerCollection.BUYERORDERNUMPRODUCTTOPTEN, map, projection, 1, 10, sorts));
	}
	/**
	 * 采购金额前十
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/buyerOrderPriceProductTopTen", method = RequestMethod.GET)
	@ResponseBody
	public Object buyerOrderPriceProductTopTen(String data, HttpServletRequest request) {
		JSONObject jsonObject = dpackData(data);
		String type = jsonObject.getString("type");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("buyerId", getUserMemberId(request));
		map.put("type", type);
		List<String> projection = new ArrayList<String>();
		projection.add("buyerId");
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		sorts.put("sort", 1);
		return Result.csuccess(MongoUtil.getListInfo(StatisticsConstant.BuyerCollection.BUYERORDERPRICEPRODUCTTOPTEN, map, projection, 1, 10, sorts));
	}
	/**
	 * 配送金额前十
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/buyerOrderPriceDispatcherTopTen", method = RequestMethod.GET)
	@ResponseBody
	public Object buyerOrderPriceDispatcherTopTen(String data, HttpServletRequest request) {
		JSONObject jsonObject = dpackData(data);
		String type = jsonObject.getString("type");
		String mongoDbCollection = "";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("buyerId", jsonObject.get("buyerId") == null ? getUserMemberId(request) : jsonObject.getString("buyerId"));
		map.put("type", type);
		if (StringUtils.strIsNotNull(jsonObject.get("productId")) ||
				StringUtils.strIsNotNull(jsonObject.get("productCode"))) {
			mongoDbCollection = StatisticsConstant.BuyerCollection.BUYERORDERPRICEDISPATCHERPRODUCTTOPTEN;
			if (StringUtils.strIsNotNull(jsonObject.get("productId"))) {
				map.put("productId", jsonObject.get("productId"));
			}
			if (StringUtils.strIsNotNull(jsonObject.get("productCode"))) {
				map.put("productCode", jsonObject.get("productCode"));
			}
		} else {
			mongoDbCollection = StatisticsConstant.BuyerCollection.BUYERORDERPRICEDISPATCHERTOPTEN;
		}
		List<String> projection = new ArrayList<String>();
		projection.add("buyerId");
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		sorts.put("sort", 1);
		int pageNo = jsonObject.get("pageNo") != null ? jsonObject.getInt("pageNo") : 1;
		int pageSize = jsonObject.get("pageSize") != null ? jsonObject.getInt("pageSize") : 10;
		return Result.csuccess(MongoUtil.getListInfo(mongoDbCollection, map, projection, pageNo, pageSize, sorts));
	}
	/**
	 * 生产企业前十
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/buyerOrderPriceProducterTopTen", method = RequestMethod.GET)
	@ResponseBody
	public Object buyerOrderPriceProducterTopTen(String data, HttpServletRequest request) {
		JSONObject jsonObject = dpackData(data);
		String type = jsonObject.getString("type");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("buyerId", getUserMemberId(request));
		map.put("type", type);
		List<String> projection = new ArrayList<String>();
		projection.add("buyerId");
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		sorts.put("sort", 1);
		return Result.csuccess(MongoUtil.getListInfo(StatisticsConstant.BuyerCollection.BUYERORDERPRICEPRODUCTERTOPTEN, map, projection, 1, 10, sorts));
	}
}
